package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.entity.Book;
import model.service.*;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

public class UserServlet extends HttpServlet {

    private Book book;
    private AddService addService;
    private String bookNo;
    private String bookName;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过form表单传递的参数
        request.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        System.out.println(method);
        if(method!=null&&method.equals("add")) {
            add(request, response);
        }
        else if(method!=null&&(method.equals("查找")||method.equals("ajax"))) {
            search(request, response);
        }else if(method!=null&&method.equals("查找所有")) {
            searchAll(request, response);
        }else if(method!=null&&method.equals("mod")) {
            modify(request, response);
        }

        //通过ajax传递的参数
        String ajax=request.getParameter("ajax");
        //System.out.println(request.getParameter("bookNo")+"---"+request.getParameter("bookName"));
        //System.out.println(ajax);
        if(ajax!=null&&ajax.equals("ajax"))
        {
            search(request, response);
        }else if(ajax!=null&&ajax.equals("all"))
        {
            searchAll(request,response);
        }else if(ajax!=null&&ajax.equals("del"))
        {
           del(request);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    public void add(HttpServletRequest request, HttpServletResponse response) {
        //获取书本详细信息
        String bookNo = request.getParameter("bookNo");
        String bookName = request.getParameter("bookName");
        String press = request.getParameter("press");
        Date pubTime = Date.valueOf(request.getParameter("pubTime"));
        String author = request.getParameter("author");
        String location = request.getParameter("location");
        double price = Double.parseDouble(request.getParameter("price"));
        boolean isBorrowed = Boolean.parseBoolean(request.getParameter("isBorrowed"));
        //封装为JavaBean
        book = new Book(bookNo, bookName, press, pubTime, author, location, price, isBorrowed);
        //实例AddServiceImpl
        addService = new AddServiceImpl();
        //获得返回信息
        String msg = addService.add(book);
        //项目路径
        String url = request.getContextPath();
        if (msg.equals("添加成功")) {
            //将提示信息放到session中，以便在成功页面取出
            HttpSession session = request.getSession();
            session.setAttribute("msg", msg);
            //跳转到添加成功页面

            try {
                response.sendRedirect(url + "/addSuccess.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            //书籍已存在，跳转到添加失败页面
           try {
                response.sendRedirect(url + "/addFail.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void search(HttpServletRequest request, HttpServletResponse response)
    {
        //实例SearchServiceImpl
        SearchService searchService=new SearchServiceImpl();
        //获取书籍编号和书籍名称
        String bookNo=request.getParameter("bookNo");
        String bookName=request.getParameter("bookName");
        //System.out.println(bookNo+":"+bookName);
        //调用service层方法，得到查询结果
        List<Book> books=searchService.search(bookNo,bookName);
        //创建jackson的核心对象，ObjectMapper
        ObjectMapper mapper=new ObjectMapper();
        //转换为json字符串
        try {
            String json = mapper.writeValueAsString(books);
            try {
                //传递到前端
                response.getWriter().print(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

    public void searchAll(HttpServletRequest request, HttpServletResponse response)
    {
        SearchAllService searchAllService=new SearchAllServiceImpl();

        List<Book> books=searchAllService.searchAll();
        //创建jackson的核心对象，ObjectMapper
        ObjectMapper mapper=new ObjectMapper();
        //转换
        try {
            String json = mapper.writeValueAsString(books);
            try {
                response.getWriter().print(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public void del(HttpServletRequest request){
        //获取书籍编号
        String bookNo=request.getParameter("bookNo");
        //调用service方法
        DelService delService=new DelServiceImpl();
        delService.del(bookNo);
    }

    public void modify(HttpServletRequest request, HttpServletResponse response) {
        //获取书籍信息
        String bookNo = request.getParameter("bookNo");
        String bookName = request.getParameter("bookName");
        String press = request.getParameter("press");
        Date pubTime = Date.valueOf(request.getParameter("pubTime"));
        String author = request.getParameter("author");
        String location = request.getParameter("location");
        double price = Double.parseDouble(request.getParameter("price"));
        boolean isBorrowed = Boolean.parseBoolean(request.getParameter("isBorrowed"));
        //封装
        book = new Book(bookNo, bookName, press, pubTime, author, location, price, isBorrowed);
        //System.out.println(book);
        //实例service层方法
        ModService modService= new ModServiceImpl();
        modService.mod(book);
        String url = request.getContextPath();
        try {
            //修改成功后，跳转
            response.sendRedirect(url + "/search.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
